package com.kingmed.kmss.framework.feign.client.dict;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpStatus;
import com.kingmed.kmss.common.dto.admin.sys.SysDataDictDTO;
import com.kingmed.kmss.common.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class DictFeignServiceFacade {

    @Autowired
    private DictFeignService dictFeignService;

    /**
     * 根据字典类型code查字典数据
     */
    public List<SysDataDictDTO> querySysDataDictByCode(String dictCode) {
        log.info("调用admin工程字典查询接口，入参[dictCode]: {}", dictCode);
        try {
            CommonResponse<List<SysDataDictDTO>> response = dictFeignService.querySysDataDictByCode(dictCode);
            log.info("调用admin工程字典查询接口成功，出参：{}", response);
            if (HttpStatus.HTTP_OK == response.getCode()) {
                return response.getData();
            }
        } catch (Exception e) {
            log.error("调用admin工程字典查询接口异常", e);
        }
        return null;
    }


    /**
     * 取出字典表所有字典数据
     */
    public List<SysDataDictDTO> queryAllSysDataDict() {
        log.info("调用admin工程字典查询接口获取所有字典数据");
        try {
            SysDataDictDTO dictDTO = new SysDataDictDTO();
            CommonResponse<List<SysDataDictDTO>> response = dictFeignService.queryAllSysDataDict(dictDTO);
            log.info("调用admin工程字典查询接口获取所有字典数据成功，出参：{}", response);
            if (HttpStatus.HTTP_OK == response.getCode()) {
                return response.getData();
            }
        } catch (Exception e) {
            log.error("调用admin工程字典查询接口获取所有字典数据异常", e);
        }
        return null;
    }

    /**
     * @return <Key, Value>
     */
    public Map<String, String> getSysDictMap(String dictCode) {
        CommonResponse<List<SysDataDictDTO>> response = dictFeignService.querySysDataDictByCode(dictCode);

        if (response.getCode() == HttpStatus.HTTP_UNAUTHORIZED || BeanUtil.isEmpty(response.getData())) {
            return new HashMap<>();
        }

        return response.getData().stream()
                .collect(Collectors.toMap(SysDataDictDTO::getDictKey, SysDataDictDTO::getDictValue));
    }

    /**
     * @return <Value, key>
     */
    public Map<String, String> getSysDictRevertMap(String dictCode) {
        CommonResponse<List<SysDataDictDTO>> response = dictFeignService.querySysDataDictByCode(dictCode);

        if (response.getCode() == HttpStatus.HTTP_UNAUTHORIZED || BeanUtil.isEmpty(response.getData())) {
            return new HashMap<>();
        }

        return response.getData().stream()
                .collect(Collectors.toMap(SysDataDictDTO::getDictValue, SysDataDictDTO::getDictKey));
    }


    public Map<String, List<SysDataDictDTO>> queryByCodeBatch(List<String> dictCodes) {
        log.info("调用admin工程字典查询接口，入参[dictCodes]: {}", dictCodes);
        try {
            CommonResponse<Map<String, List<SysDataDictDTO>>> response = dictFeignService.queryByCodeBatch(dictCodes);
            log.info("调用admin工程字典查询接口成功，出参：{}", response);
            if (HttpStatus.HTTP_OK == response.getCode()) {
                return response.getData();
            }
        } catch (Exception e) {
            log.error("调用admin工程字典查询接口异常", e);
        }
        return null;
    }


}
